package com.llxz.blog.classify.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llxz.blog.classify.entity.Banner;
import com.llxz.blog.classify.service.BannerService;
import com.llxz.blog.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classify")
public class BannerController {
    @Autowired
    BannerService bannerService;

    //前端展示
    @GetMapping("front/getBannerList/{limit}")
    public R getBannerList(@PathVariable Long limit){
        List<Banner> banners = bannerService.selectBannerList(limit);
        System.out.println(banners.size() + " ========");
        return R.ok().data("banners",banners);
    }
    //后台管理
    @GetMapping("admin/page/{current}/{limit}")
    public R page(@PathVariable Long current,@PathVariable Long limit){
        Page<Banner> page = new Page<>(current,limit);
        bannerService.page(page,null);
        return R.ok().data("items",page.getRecords()).data("total",page.getTotal());
    }

    @PostMapping("admin/add")
    public R add(@RequestBody Banner banner){
        System.out.println(banner.getId() + "==============");
        bannerService.save(banner);
        return R.ok().message("添加成功");
    }

    @GetMapping("admin/selectById/{id}")
    public R selectById(@PathVariable String id){
        Banner byId = bannerService.getById(id);
        return R.ok().data("banner",byId);
    }

    @PutMapping("admin/update")
    public R update(@RequestBody Banner banner){
        bannerService.updateById(banner);
        return R.ok().message("修改成功");
    }

    @DeleteMapping("admin/remove/{id}")
    public R remove(@PathVariable String id){
        bannerService.removeById(id);
        return R.ok().message("删除成功");
    }
}
